﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using BenefitHelper.Sys;
namespace BenefitHelper.Data.Month
{
    /// <summary>
    /// 同批次交易员月均值
    /// </summary>
    public class BatchAvgMonth
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public int BatchId { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public virtual Batch Batch { get; set; }

        public int Year { get; set; }

        public int Month { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }

        /// <summary>
        /// 资金利用率
        /// </summary>
        public double Utilization { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 涨幅
        /// </summary>
        public double RaiseRate { get; set; }

        /// <summary>
        /// 每笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }

        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }
        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }
        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }


        /// <summary>
        /// 删除当周所有记录
        /// </summary>
        /// <param name="weekId"></param>
        public void Delete(int month, int year, DB.DBManager db)
        {
            var query = from t in db.BatchAvgMonth where t.Year == year && t.Month == month select t;
            foreach (Data.Month.BatchAvgMonth c in query)
            {
                db.BatchAvgMonth.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 保存月同批次交易员均值
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitBatchMonthAvg(int month, int year, DB.DBManager db)
        {
            List<Sys.Batch> batch = db.Batches.ToList();
            foreach (Sys.Batch b in batch)
            {
                BatchAvgMonth w = new BatchAvgMonth();
                w.BatchId = b.Id;
                w.Month = month;
                w.Year = year;

                var queryExist = from t in db.BatchAvgMonth where t.BatchId == b.Id && t.Month == month && t.Year == year select t;
                if (queryExist.Count() > 0)
                {
                    w = queryExist.First();
                }


                var query = from t in db.BatchAvgDay where t.TradeHistory.MPdate == month && t.TradeHistory.YPdate == year && t.BatchId == b.Id select t;
                if (query.Count() > 0)
                {
                    w.BillCount = query.Average(a => a.BillCount);
                    w.DayCount = query.Average(a => a.DayCount);
                    w.Free = query.Sum(a => a.Free);
                    w.LoseCount = Convert.ToInt32(query.Average(a => a.LoseCount));
                    w.Profit = query.Average(a => a.Profit);
                    w.SumLost = query.Average(a => a.SumLost);
                    w.SumWin = query.Average(a => a.SumWin);
                    w.UsedMargin = query.Average(a => a.UsedMargin);
                    w.WinCount = Convert.ToInt32(query.Average(a => a.WinCount));
                    w.AvgBillBenefit = query.Average(a => a.AvgBillBenefit);
                    w.AvgPostionTime = query.Average(a => a.AvgPostionTime);
                    w.MaxReturn = query.Average(a => a.MaxReturn);
                    w.OverStopLoss = query.Average(a => a.OverStopLoss);
                    w.PingJinPing = Convert.ToInt32(query.Average(a => a.PingJinPing));
                    w.RaiseRate = query.Average(a => a.RaiseRate);
                    w.RiskCount = Convert.ToInt32(query.Average(a => a.RiskCount));
                    w.Utilization = query.Average(a => a.Utilization);
                    w.WinRate = query.Average(a => a.WinRate);
                    w.Yield = query.Average(a => a.Yield);
                    w.Month = query.First().TradeHistory.MPdate;
                    w.Year = query.First().TradeHistory.YPdate;
                    if (queryExist.Count() == 0)
                    {
                        db.BatchAvgMonth.Add(w);
                    }
                }
            }
            db.SaveChanges();
        }



    }
}